package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author 小樱
 * @version 1.0
 * @date 2023/4/27 11:07
 * @Description 预约设置Service实现类
 */
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(Integer year, Integer month) {
        //处理数据
        String start = String.format("%d-%d-1", year, month);
        String end = String.format("%d-%d-31", year, month);
        // 使用 DateTimeFormatter 将字符串转换为 LocalDate
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d");
        LocalDate startDate = LocalDate.parse(start, formatter);
        LocalDate endDate = LocalDate.parse(end, formatter);
        //查询
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(startDate, endDate);
        log.info("{}", orderSettingList);
        //转化前端需求格式
        List<Map<String, Object>> arrayList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(orderSetting.getOrderDate());
            int day = cal.get(Calendar.DAY_OF_MONTH);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("date", day);
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            arrayList.add(map);
        }
        return arrayList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date date2 = orderSetting.getOrderDate();
        LocalDate date = Instant.ofEpochMilli(date2.getTime())
                .atZone(ZoneId.systemDefault())
                .toLocalDate();
//        java.sql.Date date = java.sql.Date.valueOf(String.valueOf(date2));
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date date = sdf.parse(dateString);
        int number = orderSetting.getNumber();
        OrderSetting date1 = orderSettingDao.selectByDate(date);
        if (date1 != null) {
            orderSettingDao.editNumberByDate(date1.getId(), number);
            return;
        }
        orderSettingDao.insertByDate(date, number);
    }
    /**
     * Excel批量预约
     * @param
     */
    @Override
    @Transactional
    public void xinzeng(List<OrderSetting> orderSettings) {
        //循环操作
        for (OrderSetting orderSetting : orderSettings) {
            editNumberByDate(orderSetting.getOrderDate(), orderSetting.getNumber());
        }
}
    private void editNumberByDate(Date orderDate, int number) {
        // 判断有没有
        OrderSetting po = orderSettingDao.select(orderDate);
        if (null != po) {
            //更新
            orderSettingDao.updated(po.getId(), number);
        } else {
            //新增
            orderSettingDao.insert(new OrderSetting(orderDate, number));
        }
    }
    }
